package com.jy.adscm.wechat.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.wechat.bean.*;
import com.jy.adscm.wechat.service.WechatMaterialService;
import com.jy.adscm.wechat.service.WechatPartsService;
import com.jy.adscm.wechat.service.WechatUserService;
import com.jy.adscm.wechat.utils.RedisOperator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyl
 * @since 2019-09-29
 */
@RestController
@RequestMapping("/adscm/wechat/wechatParts")
public class WechatPartsController {
    @Autowired
    WechatPartsService wechatPartsService;
    @Autowired
    private RedisOperator redis;
    @Autowired
    WechatUserService wechatUserService;
    @RequestMapping("insert")
    public SimpleCoreResponse insert(@RequestBody WechatParts wechatParts, HttpServletRequest request)throws AdscmSystemException {
        String sessionKey = request.getParameter("sessionKey");
        String openid = redis.get(sessionKey);
        WechatUser wechatUser = wechatUserService.selectByOpenId(openid);
        wechatParts.setCreator(wechatUser.getId().toString());
        wechatParts.setCreateTime(new Date());
        wechatParts.setIsOut(0);
        wechatParts.setNums(1f);
        wechatParts.setIsSign(0);
        Boolean result=  wechatPartsService.insert(wechatParts);
        return  new SimpleCoreResponse("200","新增成功",result);
    }
    @RequestMapping("update")
    public SimpleCoreResponse update(@RequestBody WechatParts wechatParts, HttpServletRequest request)throws AdscmSystemException {
        String sessionKey = request.getParameter("sessionKey");
        String openid = redis.get(sessionKey);
        Boolean result=  wechatPartsService.updateWechatParts(wechatParts,openid);
        return  new SimpleCoreResponse("200","新增成功",result);
    }
    @RequestMapping("select")
    public SimpleCoreResponse select(@RequestBody QueryWechatParts queryWechatParts, HttpServletRequest request)throws AdscmSystemException {
        String sessionKey = request.getParameter("sessionKey");
        String openid = redis.get(sessionKey);
        queryWechatParts.setOpenid(openid);
        Page<WechatParts> page=new Page<>(queryWechatParts.getCurrentPage(),queryWechatParts.getPageSize());
        page =  wechatPartsService.selectWechatParts(page,queryWechatParts);
        return  new SimpleCoreResponse("200","新增成功",page);
    }

    @RequestMapping("selectById/{id}")
    public SimpleCoreResponse selectById(@PathVariable Long id, HttpServletRequest request)throws AdscmSystemException {
        String sessionKey = request.getParameter("sessionKey");
        String openid = redis.get(sessionKey);
        WechatParts wechatParts=  wechatPartsService.selectWechatPartsById(id);
        return  new SimpleCoreResponse("200","查询成功",wechatParts);
    }
}

